package com.pvdnc.worldmagisk;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.Callable;

public abstract class CBShell {
    private static final ALog LOG=new ALog(CBShell.class);

    private int mPort;
    public CBShell(int port){
        mPort=port;
    }

    private OutputStream mOS;

    public void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Socket socket=new Socket(InetAddress.getLocalHost(),mPort);
                    mOS= socket.getOutputStream();
                    mOS.write("id".getBytes());
                    mOS.flush();

                    InputStream is=socket.getInputStream();
                    while (true){
                        int readLength= is.available();
                        if(readLength<=0)
                            continue;
                        LOG.d("read length:"+readLength);
                        byte[] data=new byte[readLength];
                        is.read(data);
                        LOG.d("read finished");
                        onResult(data);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public abstract void onResult(byte[] resultData);
}
